<template>

</template>

<script setup>
import { getToken, setItem, getItem } from "@/utils/storage";
import useStore from "@/store";
const { app, register } = useStore();
// 判定当前用户状态
const initApp = async () => {
  app
    .getUserInfo()
    .then((res) => {
      const { status } = res;
      if (status == 2) {
        // 注销用户 - 进入注销页面
        uni.reLaunch({ url: "/pages/signout/index" });
      } else {
        // 正常用户 - 进入首页
        uni.switchTab({ url: "/pages/index/index" });
        // app.getGzhStatus();  暂时隐藏公众号接口调用相关逻辑，待后续更新 2024.9.30
      }
    })
    .catch((err) => {
      uni.switchTab({ url: "/pages/index/index" });
    });
};

// 检查登录态
const checkLogin = async () => {
  const token = getToken();
  if (!token) {
    uni.checkSession({
      success: function () {
        if (getItem("privacy")) {
          privacyConfig();
        } else {
          app
            .sureRegister()
            .then((res) => {
              privacyConfig();
            })
            .catch((err) => {
              privacyConfig();
            });
        }
      },
      fail: function () {
        privacyConfig();
      },
    });
  } else {
    privacyConfig();
  }
};

// 是否需要隐私弹窗 - 判断是否为新用户
const privacyConfig = () => {
  uni.getPrivacySetting({
    success: (res) => {
      if (res.needAuthorization) {
        // 需要弹出隐私协议
        register.getArticleConfig();
        setItem("privacy", true);
        initApp();
      } else {
        initApp();
      }
    },
    fail: () => {},
    complete: () => {},
  });
};

onLoad(() => {
  checkLogin();
});
</script >
<style lang="scss" scoped>
.app_launch {
  height: 100vh;
  /*  #ifdef H5 */
  height: calc(100vh - 44px);
  /*  #endif  */
  overflow: hidden;
}
</style>
